package ru.spb.osll.myfit.wikiverifier;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map;

import org.junit.Test;

import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableStyle;
import ru.spb.osll.myfit.fixtures.centera.ClusterListFixture;
import ru.spb.osll.myfit.fixtures.centera.SetNodeRegenerationSubmitDelayFixture;
import ru.spb.osll.myfit.fixtures.centera.WithCubetoolsFixture;
import ru.spb.osll.myfit.server.parser.PageUtils;
import ru.spb.osll.myfit.server.parser.TableElementParser;
import ru.spb.osll.myfit.util.CollectionUtils;
import ru.spb.osll.myfit.wikiverifier.result.PageVerificationResult;
import ru.spb.osll.myfit.wikiverifier.result.TableVerificationResult;
import ru.spb.osll.myfit.wikiverifier.result.VerificationResultType;

public class WikiVerifierTest {
	private WikiVerifier wikiVerifier;
	private TableElementParser m_parser = new TableElementParser();

	public WikiVerifierTest() {
		wikiVerifier = new WikiVerifier(getTemplates(), getFixtureClasses());
	}

	@SuppressWarnings("unchecked")
	public Map<String, Class<?>> getFixtureClasses() {
		return CollectionUtils.map(CollectionUtils.list(
				SetNodeRegenerationSubmitDelayFixture.class.getSimpleName(),
				WithCubetoolsFixture.class.getSimpleName(),
				ClusterListFixture.class.getSimpleName()), CollectionUtils
				.list((Class<?>) SetNodeRegenerationSubmitDelayFixture.class,
						WithCubetoolsFixture.class, ClusterListFixture.class));
	}

	public List<Table> getTemplates() {
		Table oneLineFixtreTemplate = m_parser.parseTable(CollectionUtils.list(
				"|set node regeneration submit delay|30 min|"));
		
		oneLineFixtreTemplate.setStyle(TableStyle.ONE_LINE_FIXTIRE);
		
		Table doFixtureTemplate = m_parser.parseTable(CollectionUtils.list(
				"|with cubetools|", "|write|10|files of size|10KB|using|10|threads|"));
		doFixtureTemplate.setStyle(TableStyle.DO_FIXTURE);
		
		Table setUpFixtureTemplate = m_parser.parseTable(CollectionUtils.list(
		    "|cluster list|",
				"|name|ip address|",
				"|cluster1|1.2.3.4|"));
		setUpFixtureTemplate.setStyle(TableStyle.SETUP_FIXTURE);
		
		return CollectionUtils.list(oneLineFixtreTemplate, doFixtureTemplate,
				setUpFixtureTemplate);
	}

	@Test
	public void testVerifyOk() {
		Page page = new Page();
		Table oneLineTable = m_parser.parseTable(CollectionUtils.list(
        "|set node regeneration submit delay|30 min|"));
    oneLineTable.setStyle(TableStyle.ONE_LINE_FIXTIRE);
		page.insert(oneLineTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyNotOk() {
		Page page = new Page();
	  Table oneLineTable = m_parser.parseTable(CollectionUtils.list(
        "|set nod regeneration submit delay|20 min|"));
	  oneLineTable.setStyle(TableStyle.ONE_LINE_FIXTIRE);
	  
		page.insert(oneLineTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageUtils.printSuggestion(tableResult, System.out);
				assertEquals("|set node regeneration submit delay|20 min|\n",
						PageUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyDoFixtureOk() {
		Page page = new Page();
		Table doTable = m_parser.parseTable(CollectionUtils.list(
        "|with cubetools|",
        "|write|10|files of size|10KB|using|10|threads|"));
		doTable.setStyle(TableStyle.DO_FIXTURE);
		
		page.insert(doTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyDoFixtureNotOk() {
		Page page = new Page();
		Table doTable = m_parser.parseTable(CollectionUtils.list(
        "|with cubetools|", "" +
        		"|writ|10|files o size|10KB|using|10|threads|"));
		page.insert(doTable);
	  doTable.setStyle(TableStyle.DO_FIXTURE);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|with cubetools|\n|write|10|files of size|10KB|using|10|threads|\n",
						PageUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureOk() {
		Page page = new Page();
		Table setUpTable = m_parser.parseTable(CollectionUtils.list(
        "|cluster list|",
		    "|name|ip address|", 
		    "|cluster1|1.2.3.4|"));
		setUpTable.setStyle(TableStyle.SETUP_FIXTURE);
		page.insert(setUpTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureNotOk() {
		Page page = new Page();
		Table setUpTable = m_parser.parseTable(CollectionUtils.list(
        "|cluster list|", 
        "|nam|ip addr|",
        "|cluster1|1.2.3.4|"));
		setUpTable.setStyle(TableStyle.SETUP_FIXTURE);
		page.insert(setUpTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|cluster list|\n|name|ip address|\n|cluster1|1.2.3.4|\n",
						PageUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureNotOk2() {
		Page page = new Page();
		Table setUpTable = m_parser.parseTable(CollectionUtils.list(
        "|cluster list|", 
        "|nam|ap addr|",
        "|cluster1|1.2.3.4|"));
		setUpTable.setStyle(TableStyle.SETUP_FIXTURE);
		page.insert(setUpTable);
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|cluster list|\n|name|ip address|\n|cluster1|1.2.3.4|\n",
						PageUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}
}
